<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Gather as GatherModel;
use app\common\model\Gatherreg as GatherregModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Manager as ManagerModel;

/**
 * 义工招募和社会活动控制器
 * Class Gather
 * @package app\home\controller
 */
class Gather extends Base
{
    /**
     * [getIndex  列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new GatherModel();
        $gmodel = new GatherregModel();
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        if (is_array($param)) {
            if (!empty($param['key']))
                $map['title'] = ['like', "%" . $param['key'] . "%"];
            if (!empty($param['comid']))
                $map['comid'] = $param['comid'];
        }
        $map['type'] = $param['type'];
        try{
            if(!empty($param['sontype'])){
                $map['son_states'] = 1;
            }else{
                $map['states'] = 1;
            }

            $count = $model->getWorkerCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getWorker($map,'id,title,cover,intro,starttime,endtime,comid,num,activetime,city,content',$this->from, $this->size);
            foreach($lists as $k=>$v) {
                $http = substr($v['cover'], 0, 4);
                if ($http != 'http') {
                    $lists[$k]['cover'] = config('domain_name') . $v['cover'];
                }
                if($v['starttime'] > time()){
                    $lists[$k]['state'] = '未开始';
                    $lists[$k]['state_num'] = 1;
                }
                $hasnum = $gmodel->getCount(['gather_id'=>$v['id']]);
                $lists[$k]['hasnum'] = $hasnum + $v['num'];
                if($v['starttime'] <= time() && time() <= $v['endtime']){
                    $lists[$k]['state'] = '报名中';
                    $lists[$k]['state_num'] = 2;
//                    if($v['maxnum'] <= $lists[$k]['hasnum']){
//                        $lists[$k]['state'] = '已满员';
//                        $lists[$k]['state_num'] = 3;
//                    }
                }
                if(time() > $v['endtime']){
                    $lists[$k]['state'] = '已结束';
                    $lists[$k]['state_num'] = 4;
                }
                $lists[$k]['starttime'] = date('Y-m-d',$v['starttime']);
                $lists[$k]['endtime'] = date('Y-m-d',$v['endtime']);
                $lists[$k]['activetime'] = date('Y-m-d',$v['activetime']);

            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getOne  根据id获取一条]
     * @param
     * @return json
     * @author [C。]
     */
    public function getOne()
    {
        $model = new GatherModel();
        $gmodel = new GatherregModel();
        $cmodel = new CompanyModel();
        $mmodel = new ManagerModel();
        $param = input('param.');
        $user = $this->user;
        try{
            $data = $model->findData($param['id'],'id','id,title,cover,starttime,endtime,intro,content,comid,num');
            $http = substr($data['cover'], 0, 4);
            if ($http != 'http') {
                $data['cover'] = config('domain_name') . $data['cover'];
            }
            if(time() > $data['endtime']){
                $data['is_end'] = 1;
            }else{
                $data['is_end'] = 0;
            }
            $data['starttime'] = date('Y-m-d',$data['starttime']);
            $data['endtime'] = date('Y-m-d',$data['endtime']);
            $hasnum = $gmodel->getCount(['gather_id'=>$data['id']]);
            $data['hasnum'] = $hasnum + $data['num'];
            $company = $cmodel->findData($data['comid'],'id','comname,city,qu,addr,lng,lat,phone,manager_id');
            if(!empty($company['manager_id'])){
                $manager = $mmodel->findData($company['manager_id'],'id','username');
                $company['fahao'] = $manager['username'];
            }
            $data['company'] = $company;
            if(!empty($user)){
                $reg = $gmodel->findOne(['gather_id'=>$param['id'],'memid'=>$user['id']]);
            }

            if(empty($reg)){
                $data['is_bao'] = 0;
            }else{
                $data['is_bao'] = 1;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [getApply  报名]
     * @param
     * @return json
     * @author [C。]
     */
    public function postApply()
    {
        $gmodel = new GatherregModel();
//        $model = new GatherModel();
        $param = input('param.');
        $mem = $this->user;
        if(!check_mobile($param['tel']))
            return admin_json(-2, '请填写正确手机号');
//        $gather = $model->findData($param['gather_id'],'id','num,maxnum');
//        $hasnum = $gmodel->getCount(['gather_id'=>$param['gather_id']]);
//        $num = $gather['num'] + $hasnum;
//        if($num >= $gather['maxnum']){
//            return admin_json(-4, '报名超出最大数！');
//        }
        $res = $gmodel->findData($mem['id'].'_'.$param['gather_id'],'unino');
        if($res){
            return admin_json(-3, '已经报名了');
        }
        try{
            $param['memid'] = $mem['id'];
            $param['unino'] = $mem['id'].'_'.$param['gather_id'];
            $param['addtime'] = time();
            $gmodel->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [getRecord  报名记录]
     * @param
     * @return json
     * @author [C。]
     */
    public function getRecord()
    {
        $model = new GatherregModel();
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['gr.memid'] = $mem['id'];
            $map['g.type'] = $param['type'];
            $field = 'gr.id,g.id gid,g.title,g.cover,g.starttime,g.endtime,gr.addtime';
            $count = $model->getCountes($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlists($map,$this->from, $this->size,$field);
            foreach($lists as &$v){
                $num = $model->getAllCount(['gather_id'=>$v['gid']]);
                $v['starttime'] = date('Y-m-d',$v['starttime']);
                $v['endtime'] = date('Y-m-d',$v['endtime']);
                $http = substr($v['cover'], 0, 4);
                if ($http != 'http') {
                    $v['cover'] = config('domain_name') . $v['cover'];
                }
                $v['addtime'] = MessageTime($v['addtime']);
                $v['num'] = $num;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
 * [getOne  招募详情]
 * @param
 * @return json
 * @author [C。]
 */
    public function getOnes()
    {
        $param = input('param.');
        $model = new GatherregModel;
        $gmodel = new GatherModel;
//        $cmodel = new CompanyModel;
//        $mmodel = new ManagerModel;
        try{
            $gatherreg = $model->findData($param['id'],'id');
            $gather = $gmodel->findData($gatherreg['gather_id'],'id','title,phone,endtime,starttime,addr');
//            $company = $cmodel->findData($gather['comid'],'id','addr,manager_id');
//            $manager = $mmodel->findData($company['manager_id'],'id','tel');
//            $gather['phone'] = $manager['tel'];
            $gatherreg['siyuan_addr'] = $gather['addr'];
            $gatherreg['starttime'] = date('Y-m-d',$gather['starttime']);
            $gatherreg['endtime'] = date('Y-m-d',$gather['endtime']);
            $gatherreg['phone'] = $gather['phone'];
            $gatherreg['title'] = $gather['title'];
//            $gather['sex'] = $gatherreg['sex'];
//            $gather['age'] = $gatherreg['age'];
//            $gather['baddr'] = $gatherreg['addr'];
//            $gather['addtime'] = date('Y-m-d H:i:s',$gatherreg['addtime']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$gatherreg);
    }
    /**
     * [getOne  招募详情]
     * @param
     * @return json
     * @author [C。]
     */
    public function getOnes1()
    {
        $param = input('param.');
        $model = new GatherregModel;
        try{
            $gatherreg = $model->findData($param['id'],'gather_id');
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$gatherreg);
    }
    /**
     * [add  添加法会义工]
     * @param
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new GatherModel;
        $cmodel = new CompanyModel;
        try{
            $company = $cmodel->findData($param['comid'],'id','province,city,qu,addr');
            $param['city'] = $company['province'].','.$company['city'].','.$company['qu'];
            $param['addr'] = $company['addr'];
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $param['son_states'] = 1;
            $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '保存失败'.$e->getMessage());
        }
        return admin_json(1, '保存成功');
    }
    /**
     * [edit  修改法会义工]
     * @param
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new GatherModel;
        try{
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $param['activetime'] = strtotime($param['activetime']);
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '保存失败'.$e->getMessage());
        }
        return admin_json(1, '保存成功');
    }
}